﻿/* Author:
 *  Andrea Vandin
 */
using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;

namespace TripLOAMobile.InternalEntities.ExtensionsMethods
{
    /// <summary>
    /// This class contains a set of static methods to extracts the fields of an address from a string respecting a particular format 
    /// </summary>
    public static class ExtensionsMethods
    {
        /// <summary>
        /// Determines whether [is lat long] [the specified address].
        /// </summary>
        /// <param name="address">The address.</param>
        /// <returns>
        /// 	<c>true</c> if [is lat long] [the specified address]; otherwise, <c>false</c>.
        /// </returns>
        public static bool isLatLong(this string address)
        {
            String[] latLong;
            address=address.Trim();

            if (address.IndexOf(",") == -1)
                return false;

            latLong = address.Split(',');

            if (latLong.Length != 2)
                return false;

            try
            {
                Convert.ToDouble(latLong[0]);
                Convert.ToDouble(latLong[1]);
                return true;
            }
            catch (FormatException)
            {
                return false;
            }
        }

        /// <summary>
        /// Gets the token at positino pos.
        /// </summary>
        /// <param name="address">The address.</param>
        /// <param name="pos">The pos.</param>
        /// <returns></returns>
        private static string getTokenAt(this string address, int pos)
        {
            try
            {
                address = address.Trim();
                if (address.isLatLong())
                {
                    return null;
                }
                string[] app = address.Split(',');
                if ((app == null) || (app.Length == 0))
                {
                    return null;
                }
                return app[pos];
            }
            catch (IndexOutOfRangeException)
            {
                return "";
            }
        }

        /// <summary>
        /// Gets the street.
        /// </summary>
        /// <param name="address">The address.</param>
        /// <returns></returns>
        public static string getStreet(this string address)
        {
            return address.getTokenAt(0);
        }

        /// <summary>
        /// Gets the city.
        /// </summary>
        /// <param name="address">The address.</param>
        /// <returns></returns>
        public static string getCity(this string address)
        {
            return address.getTokenAt(1);
        }

        /// <summary>
        /// Gets the state.
        /// </summary>
        /// <param name="address">The address.</param>
        /// <returns></returns>
        public static string getState(this string address)
        {
            return address.getTokenAt(3);
        }

        /// <summary>
        /// Gets the province.
        /// </summary>
        /// <param name="address">The address.</param>
        /// <returns></returns>
        public static string getProvince(this string address)
        {
            return address.getTokenAt(2);
        }

        /// <summary>
        /// Gets the street in a string with the format received from the reverse geocoding
        /// </summary>
        /// <param name="address">The address.</param>
        /// <returns></returns>
        public static string getStreetRGeoCoded(this string address)
        {
            return address.getTokenAt(0);
        }

        /// <summary>
        /// Gets the city in a string with the format received from the reverse geocoding
        /// </summary>
        /// <param name="address">The address.</param>
        /// <returns></returns>
        public static string getCityRGeoCoded(this string address)
        {
            return address.getTokenRGeoCoded(1);
        }

        /// <summary>
        /// Gets the province in a string with the format received from the reverse geocoding
        /// </summary>
        /// <param name="address">The address.</param>
        /// <returns></returns>
        public static string getProvinceRGeoCoded(this string address)
        {
            return address.getTokenRGeoCoded(2);
        }

        /// <summary>
        /// Gets the zipcode in a string with the format received from the reverse geocoding
        /// </summary>
        /// <param name="address">The address.</param>
        /// <returns></returns>
        public static string getZipCodeRGeoCoded(this string address)
        {
            string zip = address.getTokenRGeoCoded(0);
            try
            {
                Convert.ToInt32(zip);
            }
            catch (Exception)
            {
                return "";
            }
            return zip;
        }

        /// <summary>
        /// Gets the state in a string with the format received from the reverse geocoding
        /// </summary>
        /// <param name="address">The address.</param>
        /// <returns></returns>
        public static string getStateRGeoCoded(this string address)
        {
            return address.getTokenAt(2);
        }

        /// <summary>
        /// Gets the a component of an address in a string with the format received from the reverse geocoding
        /// 0=zipcode,1=city,2=province
        /// </summary>
        /// <param name="address">The address.</param>
        /// <param name="pos">The pos.</param>
        /// <returns></returns>
        private static string getTokenRGeoCoded(this string address, int pos)
        {
            try
            {
                address = address.Trim();
                if (address != "")
                {
                    if (address.isLatLong())
                    {
                        return null;
                    }
                    string[] app = address.Split(',');
                    if ((app == null) || (app.Length == 0))
                    {
                        return null;
                    }
                    string tmp = app[1].Trim();
                    app = tmp.Split(' ');
                    return app[pos];
                }
                else
                {
                    return "";
                }
            }
            catch (IndexOutOfRangeException)
            {
                return "";
            }
        }

        /// <summary>
        /// Gets the hours and minutes in minutes.
        /// </summary>
        /// <param name="dt">The dt.</param>
        /// <returns></returns>
        public static short getHoursAndMinutesInMinutes(this DateTime dt)
        {
            return (short)(dt.Hour*60+dt.Minute);
        }

    }
}
